package com.idbk.chargestation.activity.charge;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.idbk.chargestation.R;
import com.idbk.chargestation.adapter.ChargeListAdapter;
import com.idbk.chargestation.api.APIForCharge;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonChargeList;
import com.idbk.chargestation.bean.JsonPointPile;
import com.idbk.chargestation.dialog.EAlertDialog;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.GlobalResult;
import com.idbk.chargestation.util.GsonUtils;
import com.idbk.chargestation.util.UIUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 充电列表
 * @author zhx
 *
 */
public class ActivityChargeList extends BaseActivity implements OnClickListener {

	private static final int REQUEST_CODE = 1255;
	public static final int RESPONSE_CODE = 3376;
	private Context mContext;
	private ListView mListView;
	private LinearLayout mLayoutEmpty, mLayoutError;
	private List<JsonChargeList.ChargePileInfo> mData;
	private ChargeListAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_charge_list);

		initView();

		initData();
	}

	private void initView() {
		mContext = ActivityChargeList.this;
		setupToolBar2();// 显示标题栏
		mLayoutEmpty = (LinearLayout) findViewById(R.id.tj_empty_view);
		mLayoutError = (LinearLayout) findViewById(R.id.tj_error_view);
		mLayoutError.setOnClickListener(this);
		mListView = (ListView) findViewById(R.id.charge_list);
		mData = new ArrayList<JsonChargeList.ChargePileInfo>();
		mAdapter = new ChargeListAdapter(ActivityChargeList.this, mData);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				JsonChargeList.ChargePileInfo info = mData.get(position);
				// 公众号账号一个桩有多个枪在充电
				if (mData.get(position).gunInfos.size() > 1) {
					chooseGun(info);
				} else {
					int gunNum = mData.get(position).gunInfos.get(0).gunNum;
					gotoChargeInfo(info, gunNum);
				}
			}
		});
	}
	
	private void chooseGun(final JsonChargeList.ChargePileInfo result) {
		List<CharSequence> infoList = new ArrayList<CharSequence>();
		for (int i = 0; i < result.gunInfos.size(); i++) {
			infoList.add("枪 - " + result.gunInfos.get(i).gunNum);
		}
		new EAlertDialog(ActivityChargeList.this)
		.setItems(infoList, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				int gunNum = result.gunInfos.get(which).gunNum;
				gotoChargeInfo(result, gunNum);
			}
		})
		.show();
	}

	private void gotoChargeInfo(JsonChargeList.ChargePileInfo result, int gunNum) {
		// 判断 直流/交流单相/交流三相
		if (result.pileType == JsonPointPile.PILE_TYPE_DC
				|| result.pileType == JsonPointPile.PILE_TYPE_DC_DOUBLE_GUN_ASYN
				|| result.pileType == JsonPointPile.PILE_TYPE_DC_DOUBLE_GUN_SYN
				|| result.pileType == JsonPointPile.PILE_TYPE_DC_MULTI_GUN_SYN) {
			// 直流
			Intent intent = new Intent(mContext, ActivityChargingDc.class);
			// 需要传一些基本的数据进去
			intent.putExtra("pileSn", result.pileSn);
			intent.putExtra("gunNum", gunNum);
			intent.putExtra(ActivityChargingDc.KEY_CHARGE_ACTION,
					ActivityChargingDc.CHARGE_ACTION_CHARGING);
			startActivityForResult(intent, REQUEST_CODE);

		} else if (result.pileType == JsonPointPile.PILE_TYPE_AC_SINGLE_PHASE
				|| result.pileType == JsonPointPile.PILE_TYPE_AC_THREE_PHASE
				|| result.pileType == JsonPointPile.PILE_TYPE_AC_DOUBLE_GUN_SINGLE_PHASE
				|| result.pileType == JsonPointPile.PILE_TYPE_AC_DOUBLE_GUN_THREE_PHASE
				|| result.pileType == JsonPointPile.PILE_TYPE_AC_MULTI_GUN_SINGLE_SYN
				|| result.pileType == JsonPointPile.PILE_TYPE_AC_MULTI_GUN_THREE_SYN) {
			// 交流
			Intent intent = new Intent(mContext, ActivityChargingAc.class);
			// 需要传一些基本的数据进去
			intent.putExtra("pileSn", result.pileSn);
			intent.putExtra("gunNum", gunNum);
			intent.putExtra(ActivityChargingAc.KEY_CHARGE_ACTION,
					ActivityChargingAc.CHARGE_ACTION_CHARGING);
			startActivityForResult(intent, REQUEST_CODE);

		} else {
			Toast.makeText(mContext, "无法判断充电桩类型！", Toast.LENGTH_SHORT).show();
		}
	}

	private void initData() {
		showMyProgressDialog(false, false, "正在读取...");
		mMainRequest = APIForCharge.getChargeList(mChargeResponse);
	}

	EHttpResponse mChargeResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonChargeList result = GsonUtils.toBean(JsonChargeList.class,
					response);

			if (handleResponseStatus(result)) {
				mData.clear();
				if (result.pileInfos != null && result.pileInfos.size() > 0) {
					UIUtil.hideAll(mLayoutEmpty, mLayoutError);
					mListView.setVisibility(View.VISIBLE);
					mData.addAll(result.pileInfos);
					mAdapter.notifyDataSetChanged();

				} else {
					UIUtil.showEmpty(mLayoutEmpty, mLayoutError);
					mListView.setVisibility(View.GONE);
				}
			} else if (result.status == GlobalResult.CHARGE_FINISH.getStatus()){
				UIUtil.showEmpty(mLayoutEmpty, mLayoutError);
				mListView.setVisibility(View.GONE);
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			Toast.makeText(mContext, "无法读取数据！", Toast.LENGTH_SHORT).show();
			UIUtil.showNetError(mLayoutEmpty, mLayoutError);
			mListView.setVisibility(View.GONE);
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
			dismissMyProgressDialog();
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tj_error_view:
			initData();
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQUEST_CODE && resultCode == RESPONSE_CODE) {
			initData();
		}
	}
}
